//该文件专门用于创建整个应用的路由器
import VueRouter from "vue-router";
//应用组件
import home from "@/pages/home.vue";
import login from "@/pages/login.vue";
import H_message from "@/pages/H_message.vue"
import H_News from "@/pages/H_News.vue"
import H_M_Detail from "@/pages/H_M_Detail.vue"
//创建一个路由器 并暴露
//创建router实例对象，去管理一组一组的路由规则
const routre = new VueRouter({
    routes: [{
        name: "主页", path: "/home", component: home, meta: {title: "主页"}, children: [//通过children配置子级路由
            {
                meta: {isAuth: true, title: "信息"}, name: "H_message", path: "H_message",//此处一定定不要写: /H_message
                component: H_message, children: [{
                    meta: {title: "详细信息"}, name: "详细信息",//给路由命名
                    path: "H_M_Detail",//使用占位符声明接收 params 参数
                    component: H_M_Detail, //props的第一种写法,值为对象,该对象中的所有key-value都会以props的形式传给Detail组件
                    props($route) {
                        return {id: $route.query.id, title: $route.query.title}
                    }
                }]
            }, {
                name: "H_News", path: "H_News", component: H_News, meta: {isAuth: true, title: "轮播图"},
            }]
    }, {
        meta: {title: "登录页"}, name: "login", path: "/login", component: login
    },]
})



//全局前置路由守卫-每次路由切换之前被调用-初始化的时候被调用
routre.beforeEach((to, from, next) => {
    //console.log(to, from)//to 跳转的路径 from当前路径

    if (to.meta.isAuth) {//判断是否需要权限
        if (localStorage.getItem('school') == "zouyangguang") {
            next()
        } else {
            console.log("school 错误")
        }
    } else {
        next()
    }
})
//全局后置路由守卫-每次路由切换之后被调用-初始化的时候被调用
routre.afterEach((to, from)=>{
    document.title=to.meta.title||"陈文俊"
})
export default routre